package com.lkx.farmershop.controller;

import com.lkx.farmershop.common.api.ApiResult;
import com.lkx.farmershop.model.dto.ExitGroupDto;
import com.lkx.farmershop.model.dto.HomeRequestDto;
import com.lkx.farmershop.model.dto.TuanDto;
import com.lkx.farmershop.model.entity.JoinReasonInfo;
import com.lkx.farmershop.model.vo.DraftsVo;
import com.lkx.farmershop.model.vo.GroupDetailVo;
import com.lkx.farmershop.model.vo.GroupVo;
import com.lkx.farmershop.model.vo.JoinApplyVo;
import com.lkx.farmershop.service.TuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tuan")
public class TuanController {

    @Autowired
    private TuanService tuanService;

    /**
     * 保存草稿
     * @param tuanDto
     * @return
     */
    @PostMapping("/saveDraft")
    @Transactional
    public ApiResult<String> saveDraft(@RequestBody TuanDto tuanDto) {
        return tuanService.saveDraft(tuanDto);
    }

    /**
     * 发布组团
     * @param tuanDto
     * @return
     */
    @PostMapping("/sendTuan")
    @Transactional
    public ApiResult<String> sendTuan(@RequestBody TuanDto tuanDto) {
        return tuanService.sendTuan(tuanDto);
    }

    /**
     * 获取草稿信息列表
     * @param userId
     * @return
     */
    @GetMapping("/getDrafts")
    public ApiResult<List<DraftsVo>> getDrafts(@RequestParam("userId") Long userId) {
        return tuanService.getDrafts(userId);
    }

    /**
     * 获取我发布的组团信息列表
     * @param userId
     * @return
     */
    @GetMapping("/getSend")
    public ApiResult<List<DraftsVo>> getSend(@RequestParam("userId") Long userId) {
        return tuanService.getSend(userId);
    }

    // 在我发布的或我暂存的获取组团信息详情
    @GetMapping("/getTuanById")
    public ApiResult<TuanDto> getTuanById(@RequestParam("tuanId") Long tuanId) {
        return tuanService.getTuanById(tuanId);
    }

    //删除草稿
    @DeleteMapping("/deleteDraft")
    @Transactional
    public ApiResult<String> deleteDraft(@RequestParam("tuanId") Long tuanId) {
        return tuanService.deleteDraft(tuanId);
    }

    /**
     * 主页获取组团信息
     * @param homeRequestDto
     * @return
     */
    @PostMapping("/getGroupList")
    public ApiResult<List<GroupVo>> getGroupList(@RequestBody HomeRequestDto homeRequestDto) {
        return tuanService.getGroupList(homeRequestDto);
    }

    /**
     * 点击组团查看详情
     * @param tuanId
     * @return
     */
    @GetMapping("/getGroupById")
    public ApiResult<GroupDetailVo> getGroupById(@RequestParam("tuanId") Long tuanId, @RequestParam("userId") Long userId) {
        return tuanService.getGroupById(tuanId, userId);
    }

    /**
     * 申请加入组团
     * @param joinReasonInfo
     * @return
     */
    @Transactional
    @PostMapping("/joinGroup")
    public ApiResult<String> joinGroup(@RequestBody JoinReasonInfo joinReasonInfo) {
        return tuanService.joinGroup(joinReasonInfo);
    }

    /**
     * 获取我加入的组团
     * @param userId
     * @return
     */
    @GetMapping("/getMyJoin")
    public ApiResult<List<DraftsVo>> getMyJoin(@RequestParam("userId") Long userId) {
        return tuanService.getMyJoin(userId);
    }


    /**
     * 退出组团
     * @param exitGroupDto
     * @return
     */
    @Transactional
    @DeleteMapping("/exitGroup")
    public ApiResult<String> exitGroup(@RequestBody ExitGroupDto exitGroupDto) {
        return tuanService.exitGroup(exitGroupDto);
    }

    /**
     * 结束组团
     * @param tuanId
     * @return
     */
    @PutMapping("/endTuan")
    public ApiResult<String> endTuan(@RequestParam("tuanId") Long tuanId) {
        return tuanService.endTuan(tuanId);
    }

    /**
     * 获取加入组团的申请信息
     * @param tuanId
     * @return
     */
    @GetMapping("/getJoinInfo")
    public ApiResult<List<JoinApplyVo>> getJoinInfo(@RequestParam("tuanId") Long tuanId) {
        return tuanService.getJoinInfo(tuanId);
    }


}
